<?php

namespace App\Http\Controllers\Admin\User;

use App\Exceptions\LoginException;
use App\Services\User\UserService;
use Exception;
use Illuminate\Http\JsonResponse;
use Random\RandomException;

class UserController extends BaseUserController
{
    private UserService $userService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        $this->userService = loadService('User\UserService');
        parent::__construct();
    }

    /**
     * 用户详情
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function userInfo(): JsonResponse
    {
        $userInfo = $this->userService->userInfo();
        return outputJsonResult($userInfo);
    }

    /**
     * 用户列表
     *
     * @return JsonResponse
     */
    public function userList(): JsonResponse
    {
        $userList = $this->userService->userList($this->params);
        return outputJsonResult($userList);
    }

    /**
     * 设置用户角色
     *
     * @return JsonResponse
     */
    public function setUserAuthorities(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->setUserAuthorities($this->params);
        return outputJsonResult();
    }

    /**
     * 重置密码
     *
     * @return JsonResponse
     * @throws LoginException
     */
    public function resetPassword(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->resetPassword($this->params);
        return outputJsonResult();
    }

    /**
     * 删除用户
     *
     * @return JsonResponse
     * @throws LoginException
     */
    public function deleteUser(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->deleteUser($this->params);
        return outputJsonResult();
    }

    /**
     * 设置自身信息
     *
     * @return JsonResponse
     */
    public function setSelfInfo(): JsonResponse
    {
        $this->userService->setSelfInfo($this->params);
        return outputJsonResult();
    }

    /**
     * 修改密码
     *
     * @return JsonResponse
     */
    public function changePassword(): JsonResponse
    {
        $this->userService->changePassword($this->params);
        return outputJsonResult();
    }

    /**
     * 注册
     *
     * @return JsonResponse
     * @throws LoginException
     * @throws RandomException
     */
    public function createUser(): JsonResponse
    {
        $this->userService->createUser($this->params);
        return outputJsonResult();
    }

    /**
     * 设置用户信息
     *
     * @return JsonResponse
     */
    public function setUserInfo(): JsonResponse
    {
        $this->userService->setUserInfo($this->params);
        return outputJsonResult();
    }
}
